package com.onehash.onebilling.gui;

import java.awt.*;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class GenericDialog extends JDialog {
    JPanel JPcontent = new JPanel();


    public GenericDialog(Frame owner, String title, boolean modal, Dimension dimension, JPanel panel) {
        super(owner, title, modal);
        try {
            jbInit(dimension, panel);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit(Dimension dimension, JPanel panel) throws Exception {
        this.setDefaultCloseOperation(javax.swing.WindowConstants.
                                      DO_NOTHING_ON_CLOSE);
        this.setResizable(false);
        this.setMinimumSize(dimension);
        this.setPreferredSize(dimension);
        this.addWindowListener(new GenericDialog_this_windowAdapter(this));
        this.getContentPane().add(panel, java.awt.BorderLayout.NORTH);
        this.setSize(dimension);
        this.center();
    }

    public void addPanel(JPanel panel){
        /* got to remove the JPcontent panel and add customized ones! */
        this.getContentPane().remove(this.JPcontent);
        this.getContentPane().add(panel);
    }

    private void center(){
        //center the dialog
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height)
            frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
            frameSize.width = screenSize.width;
        this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    public void this_windowClosing(WindowEvent e) {
        int ans = MessageDialog.showQuestionDialog((Frame)this.getOwner(),
                                                   this.getTitle(),
                //"Any changes made will be cancelled. Do you want to exit?");
        		"Do you want to exit?");
        if (ans == MessageDialog.YES_OPTION) {
            this.dispose();
        }
    }

}


class GenericDialog_this_windowAdapter extends WindowAdapter {
    private GenericDialog adaptee;
    GenericDialog_this_windowAdapter(GenericDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void windowClosing(WindowEvent e) {
        adaptee.this_windowClosing(e);
    }
}
